//
//  LogingMiddleware.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/4/19.
//

class LoggingMiddleware: RequestMiddleware {
    func handleRequest<T: API>(_ api: T, body: Data) throws -> (Data, T) {
        Logger.log(.debug, message: "请求 \(api.method.rawValue) 路径: \(api.path)", tag: .netWork)
        Logger.log(.debug, message: "请求头: \(api.headers.description)", tag: .netWork)
        if let bodyString = String(data: body, encoding: .utf8) {
            Logger.log(.debug, message: "请求body: \(bodyString)", tag: .netWork)
        }

        return (body, api)
    }
    
    func handleResponse<T: API>(_ api: T, data: Data, error: Error?) -> Data {
        if let error = error {
            Logger.log(.error, message: "\(error.localizedDescription)", tag: .netWork)
        } else {
            Logger.log(.debug, message: "响应 Response: \(String(data: data, encoding: .utf8) ?? "No Data")", tag: .netWork)
        }
        return data
    }
}
